<?php
namespace app\index\controller;

use app\index\controller\Base;
use app\index\model\Money;
use app\index\model\User;
use app\index\model\Apply;
use think\Request;

class My extends Base
{
    public function index(){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
            "user"=>$user,
        );
        return view("index",$data);
    }
    public function info(){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
            "user"=>$user,
        );
        return view("info",$data);
    }

    public function apply(){
        $user = $this->user_info();
        $Applys = Apply::where(array("userid"=>$user['id']))->select();
        $data = array(
            "setting" => $this->getAllSetting(),
            "qrcode"=>$this->getSetting("teacher_qrcode"),
            "apply" => $Applys,
        );
        return view("apply",$data);
    }
    /*
     * 我要报名的表单
     */
    public function register(){
       $user = $this->user_info();
       return view('register',['data'=>$user]);
    }
    /*
     * 保存报名信息
     */
    public function saveRegister(Request $request){
      //判断数据库中user表的数据是否完整，如果不完整补充完整
      $user = user::where(['id'=>$this->user_info()['id']])->find();
      $data =$request->post();
      if($user['name'] == ""){ user::update(['name'=>$data['name'],'id'=>$user['id']]);}
      if($user['sex'] == ""){ user::update(['sex'=>$data['sex'],'id'=>$user['id']]);}
      if($user['phone'] == ""){ user::update(['phone'=>$data['phone'],'id'=>$user['id']]);}
      if($user['nation'] == ""){ user::update(['nation'=>$data['nation'],'id'=>$user['id']]);}
      if($user['qqwx'] == ""){ user::update(['qqwx'=>$data['qqwx'],'id'=>$user['id']]);}
      if($user['parenttel'] == ""){ user::update(['parenttel'=>$data['parenttel'],'id'=>$user['id']]);}
      if($user['grade'] == ""){ user::update(['grade'=>$data['grade'],'id'=>$user['id']]);}
      if($user['address'] == ""){ user::update(['address'=>$data['address'],'id'=>$user['id']]);}
      if($user['update_time'] == ""){ user::update(['update_time'=>time(),'id'=>$user['id']]);}
      //查询是否有未处理完的报名信息，如果有不允许报名
      if (apply::where("userid={$user['id']}")->find()){
          $this->error("录入失败，您已过信息！","My/apply");
      }else {
        //添加报名记录
        $res = apply::create([
          'userid' => $user['id']
          , 'major' => $data['major']
          , 'create_time' => time()
          , 'update_time' => time()
          , 'comment' => $data['comment']
          , 'status' => 2
          , 'type' => 2
          , "grade" => $data['grade']
        ]);

        if ($res) {
          $this->success("录入成功", "My/apply");
        } else {
          $this->error("录入失败，请联系工作人员", "");
        }
      }
    }
    /*
     * 显示付款的二维码页面
     */
    public function pay(Request $request){
      $id = $request->get('id');
      $data = array(
        "setting" => $this->getAllSetting(),
        "id"=>$id

      );
      return view("pay",$data);
    }
    /*
     * 完成支付
     */
    public function finishPay(Request $request){
      $data = $request->post();
      $data['status']=1;
      $data['type']=1;
      $data['pay_time']=time();
      $res = Apply::update($data);
      if ($res) {
        $this->success("操作成功，等待核对", "My/apply");
      } else {
        $this->error("操作失败，请联系工作人员", "Indexs/index");
      }
    }
    /*
     * 录取查询
     */
    public function search(Request $request){
      $apply = "";
      $data="";
      if($request->isPost()) {
        $data = $request->post();
        $res = User::where(["name" => $data['name'], "idcard" => $data['idcard']])->find();
        $apply = Apply::where(['userid' => $res['id']])->select();
      }
      $data = array(
        "setting" => $this->getAllSetting(),
        "apply"=>$apply,
        "data"=>$data
      );
      return view("search",$data);
    }
    /*
     * 须知
     */
    public function know(){
      $data = array(
        "setting" => $this->getAllSetting(),
        'text'=>$this->getAllText()

      );
      return view("know",$data);
    }
    /*
     * 通知书
     */
    public function tzs(Request $request){
      $apply = Apply::get($request->get("id"));
      $data = array(
        "setting" => $this->getAllSetting(),
        'text'=>$this->getAllText(),
        "apply"=>$apply,
        "user"=>User::get($apply['userid'])
      );
      return view('tzs',$data);
    }
    /*
     * 缴费记录
     */
    public function money(){
      $user = $this->user_info();
      $money = Money::where("userid",$user['id'])->select();
      $arr = [
        "setting"=>$this->getAllSetting(),
        "money"=>$money
      ];
      return view("money",$arr);
    }

}
